package com.iseastar.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.AppConfig;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.R;

import droid.frame.view.TextWatcherExt;

/**
 * 初次登录, 修改默认密码
 * 
 * @author coffee<br>
 *         2015-3-27上午11:03:33
 */
public class PasswordChangeActivity extends BaseActivity2 implements OnClickListener {

	private EditText mPassword1;
	private EditText mPassword2;
	private Button mConfrimButton;
	private int type;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Intent intent = getIntent();
		type = intent.getIntExtra("tpye", 1);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void findViewById() {
		setContentView(R.layout.password_change);
		super.findViewById();
		getAppTitle().setCommonTitle("修改密码");
		mPassword1 = (EditText) findViewById(R.id.password_1);
		mPassword2 = (EditText) findViewById(R.id.password_2);
		mConfrimButton = (Button) findViewById(R.id.password_sure);
		// 改变确认修改按钮的样式
		setButtonEnableStyle(mConfrimButton, false);
		mPassword2.addTextChangedListener(new TextWatcherExt() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.length() >= 6) {
					setButtonEnableStyle(mConfrimButton, true);
				} else {
					setButtonEnableStyle(mConfrimButton, false);
				}
			}
		});
		mConfrimButton.setOnClickListener(this);

	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case MsgID.login_password_change:
		case MsgID.login_password_change + 1:
			ReqResult<?> result = JSON.parser(msg.obj);
			if (checkLoginStatus(result)) {
				showToast("修改成功");
				UserBean user = AppCache.getUser();
				user.setPassword(mPassword1.getText().toString());
				AppCache.setUser(user);
				finish();
			} else {
				showToast(result.getMessage());
			}
			return true;
		default:
			return super.handleMessage(msg);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.password_sure:
			UserBean user = AppCache.getUser();
			if (mPassword1.length() < 6) {
				mPassword1.requestFocus();
				mPassword1.setError("请输入6-12位密码");
				return;
			}
			if (mPassword2.length() < 6) {
				mPassword2.requestFocus();
				mPassword2.setError("请输入6-12位密码");
				return;
			}
			// if (mPassword1.getText().toString().equals(mPassword2.getText().toString()) == false) {
			// mPassword2.requestFocus();
			// mPassword2.setError("两次密码不一致");
			// return;
			// }
			//
			if (mPassword2.getText().toString().equals(AppConfig.initPassword)) {
				mPassword2.requestFocus();
				mPassword2.setError("新密码不能与初始密码相同");
				return;
			}
			if (!mPassword1.getText().toString().equals(user.getPassword())) {
				mPassword1.requestFocus();
				mPassword1.setError("原始密码错误");
				return;
			}
			showLoadingDialog("");
			if (type == 0) {
				AppHttp.getInstance().loginPasswordChange(user.getId(), mPassword1.getText().toString(), true);
			} else {
				AppHttp.getInstance().loginPasswordChange(user.getId(), mPassword2.getText().toString(), false);
			}
			break;

		default:
			break;
		}

	}
}
